// pages/startup/startup.js
const app = getApp();
Page({
    data: {
        userInfo:{},
        showAuth:true,
        Uid:''
    },
    // 获取用户授权信息
    auth:function(e){
        var that = this;
        console.log(e.detail.userInfo)
        wx.getUserProfile({
            desc:'必须授权才可以继续使用',
            success:res=>{
                console.log('授权成功',res)
                this.setData({
                    userInfo:e.detail.userInfo
                })
                this.uploadData(res.userInfo.avatarUrl,res.userInfo.nickName);
                wx.setStorageSync('Uname',res.userInfo.nickName)
                setTimeout(function(){
                    wx.switchTab({
                        url:'/pages/community/community'
                    })
                },1500)
            },
            fail:res=>{
                console.log('授权失败',res)
            }
        })  
        
        //获取用户唯一openId
        wx.login({
            success: res => {
                if (res.code) {
                    wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxb3847d7cdd56f109&secret=e8f101033b4236755b648dbbbfaefbd7&js_code=' + res.code + '&grant_type=authorization_code',
                        data: {},
                        header: {
                            'content-type': 'json'
                        },
                        success: function (res) {
                            console.log(res);
                            console.log('openid为' + res.data.openid);
                            that.setData({
                                Uid:res.data.openid
                            })
                       
                        }
                    })
                } else {
                    console.log('登录失败' + res.errMsg)
                }
            }
        })
    },
    uploadData:function(uimage,uname) {  /* 将用户信息上传到数据库 */
        var that = this;
      let value = this.data.userInfo;   
      wx.request({
        url: `${app.globalData.url}/users/my-home/my-account`,//写自己的服务器
        header: { "Content-Type": "application/x-www-form-urlencoded" },
        method: "POST",
        data: {
          Uid:that.data.Uid,
          Uname:uname,
          Usex:1,
          Uday:'',
          Uimage:uimage,
          Uphone:'',
          Uemail:'221982@qq.com',
          Pid:'',
          Uarea:'',
          Ulevel: 0
          
        },
        success: function (res) { 
          console.log(res.data) ;
          wx.setStorage({
              data:that.data.Uid,
              key:'Uid'
          })
          wx.setStorage({
            data: uimage,
            key: 'Uimage',
          })
        },
        fail: function (res) {
           console.log("fail")
           console.log(res)          
        }
  
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    onhometab:function(){
        let myid = wx.getStorageSync('Uid');
        if(!myid) {
            wx.showModal({
                title: '提示',
                content: '请先进行微信授权',
                showCancel: true, 
                cancelText: "取消", 
                cancelColor: 'lightsalmon', 
                confirmText: "确定", 
                confirmColor: 'lightsalmon', 
        
            })
        }
        else {
            wx.switchTab({
                url:'/pages/community/community'
            })
        }
        
    }
})